import grails.converters.JSON

class TrainProgrammeController {
    def authenticateService

    static allowedMethods = [save: "POST", update: "POST"]

    def index = {
        redirect(action: "list", params: params)
    }

    def list = {
        params.max = 50
        if (params.id && params.course) { 

            // 显示所有教学计划
            [trainProgrammeInstanceList: TrainProgramme.withCriteria {
                and{
                    eq('teacher' , authenticateService?.userDomain()) 
                    eq('classes' , Classes.get(params.id)) 
                    eq('course' , params.course) 
                }  
                order('week' , 'asc');
            },
                trainProgrammeInstanceTotal : TrainProgramme.withCriteria {
                and{
                    eq('teacher' , authenticateService?.userDomain()) 
                    eq('classes' , Classes.get(params.id)) 
                    eq('course' , params.course) 
                } 
                count('id')
            } , 
                'classesid' : params.id,
                'course' : params.course ,
                'schoolid' : session?.school?.id ,
                'teacher' : authenticateService?.userDomain()?.id
            ]
        }else{
             [trainProgrammeInstanceList: [],
                trainProgrammeInstanceTotal : 0 ]
        }
    }

    def create = {
        def trainProgrammeInstance = new TrainProgramme()
        trainProgrammeInstance.properties = params
        return [trainProgrammeInstance: trainProgrammeInstance]
    }

    def save = {
        def trainProgrammeInstance = new TrainProgramme(params)
        if (trainProgrammeInstance.save(flush: true)) {
            flash.message = "${message(code: 'default.created.message', args: [message(code: 'trainProgramme.label', default: 'TrainProgramme'), trainProgrammeInstance])}"
            redirect(action: "list")
        }
        else {
            render(view: "create", model: [trainProgrammeInstance: trainProgrammeInstance])
        }
    }

    def show = {
        def trainProgrammeInstance = TrainProgramme.get(params.id)
        if (!trainProgrammeInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'trainProgramme.label', default: 'TrainProgramme'), params.id])}"
            redirect(action: "list")
        }
        else {
            [trainProgrammeInstance: trainProgrammeInstance]
        }
    }

    def edit = {
        def trainProgrammeInstance =new  TrainProgramme(params) 
        if (!trainProgrammeInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'trainProgramme.label', default: 'TrainProgramme'), params.id])}"
            redirect(action: "list")
        }
        else {
            return [trainProgrammeInstance: trainProgrammeInstance]
        }
    }

    def update = {
   
        def trainProgrammeInstance
        if (params.id) {
            trainProgrammeInstance = TrainProgramme.get(params.id)
        }else{
            
           trainProgrammeInstance = new TrainProgramme(params) 
           trainProgrammeInstance.description = null
           trainProgrammeInstance = TrainProgramme.find(trainProgrammeInstance)
           if (!trainProgrammeInstance) {
               trainProgrammeInstance = new TrainProgramme()
           }
        }
        
        if (trainProgrammeInstance) { 
            trainProgrammeInstance.properties = params
            trainProgrammeInstance.school = session.school
            trainProgrammeInstance.teacher = authenticateService?.userDomain()
            if (!trainProgrammeInstance.hasErrors() && trainProgrammeInstance.save(flush: true)) {
            
                return render(['success': true ,'message' : "${message(code: 'default.updated.message', args: [message(code: 'trainProgramme.label', default: 'TrainProgramme'), trainProgrammeInstance])}"] as JSON) 
            }
            else {
               
                return render(['success': false ,'message' : '添加失败！'] as JSON) 
            }
        }
        else {
            return render(['success': false ,'message' : '没有找到！'] as JSON) 
        }
    }

    def delete = {
        def trainProgrammeInstance = TrainProgramme.get(params.id)
        if (trainProgrammeInstance) {
            try {
                trainProgrammeInstance.delete(flush: true)
                flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'trainProgramme.label', default: 'TrainProgramme'), params.id])}"
                redirect(action: "list")
            }
            catch (org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'trainProgramme.label', default: 'TrainProgramme'), params.id])}"
                redirect(action: "show", id: params.id)
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'trainProgramme.label', default: 'TrainProgramme'), params.id])}"
            redirect(action: "list")
        }
    }
}
